<script>
import goods from "@/store/goods";

export default {
    name: "AddGoods",
    methods:{
        submitHandler(){
            const goodsName = this.$refs.goodsName.value.trim();
            const goodsPrice = this.$refs.goodsPrice.value.trim();
            if(goodsName.length>0 && goodsPrice.length>0){
                // 1
                // if(this.$store.state.goods.goodsList.some(v=>v.goodsName===goodsName)){
                //     alert("商品名称不允许重复");
                //     return;
                // }
                // 要执行仓库中的名字为ADD_GOODS的mutation函数
                this.$store.commit("ADD_GOODS",{
                    goodsName,
                    goodsPrice:goodsPrice/1,
                    success:()=>{
                        this.$router.push("/goodsList");
                    }
                });
                // this.$router.push("/goodsList");
            }else{
                alert("请输入商品名称或价格");
            }

        }
    }
}
</script>

<template>
    <form>
        <p>商品名称：<input ref="goodsName" type="text"></p>
        <p>商品价格：<input ref="goodsPrice" type="text"></p>
        <button @click.prevent="submitHandler">提交</button>
    </form>
</template>

<style scoped>

</style>